// import { numberMakeZore } from '../../utils/index'
// import { dateFormat, formatDateCh } from '../../utils/util'
// const ONE_DAY_TIMESTAMP = 24 * 3600 * 1000

import { formatDate } from '../../utils/util'
import { _t } from '../../i18n/base'
function generatedMonthCalendar(minDate, year, month) {
  const TODAY_TIMESTAMP = minDate.setHours(0, 0, 0, 0)
  const targetDate = new Date(year, month, 1)
  const currentYear = targetDate.getFullYear()
  let currentMonth = targetDate.getMonth() + 1
  const currentEndDate = new Date(new Date(year, month + 1, 0)).getDate()
  const result = {
    value:
      `${currentYear}-` +
      (currentMonth > 9 ? currentMonth : '0' + currentMonth),
    valueCN: `${currentYear}-${currentMonth}`,
    children: [],
  }

  for (let index = 0; index < currentEndDate; index++) {
    const tempDate = new Date(year, month, index + 1)
    const timestamp = tempDate.setHours(0, 0, 0, 0)
    let date = tempDate.getDate()
    const day = tempDate.getDay()
    const marginLeft =
      date === 1 && day > 0
        ? `margin-left: calc((88rpx + ((100% - 88rpx * 7) / 6)) * ${day})`
        : ''
    const marginRight = day === 0 ? 'margin-left: 0' : ''
    result.children.push({
      value: `${result.value}-` + (date > 9 ? date : '0' + date),
      date,
      day,
      isOverToday: timestamp < TODAY_TIMESTAMP,
      style: `${marginLeft}${marginRight}`,
    })
  }
  return result
}

function generatedCalendar(minDate) {
  const result = []
  minDate = minDate.replace(/-/g, '/')
  const min = new Date(minDate)
  const minMonth = min.getMonth()
  let setDate = new Date()
  setDate.setDate(1)
  setDate.setMonth(minMonth - 1)
  for (let index = 0; index < 12; index++) {
    setDate.setMonth(setDate.getMonth() + 1)
    const year = setDate.getFullYear()
    const month = setDate.getMonth()
    result.push(generatedMonthCalendar(min, year, month))
  }
  return result
}

Component({
  properties: {
    show: {
      type: Boolean,
      value: false,
    },

    zIndex: {
      type: Number,
      value: 99999,
    },
    minDate: {
      type: String,
      value: '',
    },
  },

  data: {
    calendar: '',
    showCalendar: false,
    weeks: [],
    lang: {},
    top: 300,
  },

  methods: {
    // 关闭弹窗
    close() {
      this.triggerEvent('close')
    },
    // 点击
    clickDay(e) {
      let item = e.currentTarget.dataset.item
      this.setData({
        date: item.value,
      })
    },
    // 确认
    confirm() {
      this.triggerEvent('confirm', { date: this.data.date })
    },
    initLanguage() {
      let weeks = []
      const weeksZH = ['日', '一', '二', '三', '四', '五', '六']
      const weeksTW = ['日', '一', '二', '三', '四', '五', '六']
      const weeksEN = ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat']
      let language = wx.getStorageSync('language')
      if (language == 'zh') {
        weeks = weeksZH
      }
      if (language == 'tw') {
        weeks = weeksTW
      }
      if (language == 'en') {
        weeks = weeksEN
      }
      return weeks
    },
    setLang() {
      let weeks = this.initLanguage()
      const targetDate = new Date()
      const currentYear = targetDate.getFullYear()
      const currentMonth = targetDate.getMonth() + 1
      const currentDay = targetDate.getDate()
      if (!this.data.minDate) {
        this.setData({
          minDate: `${currentYear}-${currentMonth}-${currentDay}`,
        })
      }
      let calendar = generatedCalendar(this.data.minDate)
      this.setData({
        weeks,
        calendar,
        lang: _t(),
        date: formatDate(new Date()),
      })
      setTimeout(() => {
        this.setData({
          showMonth: `MY${currentYear}-${currentMonth}`,
        })
      }, 100)
    },
  },
  lifetimes: {
    attached() {
      // 在组件实例进入页面节点树时执行
      this.setLang()
    },
    detached() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  created() {},
})
